package com.example.yichuangyun.mvvm.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ItemOrderDetailBaseInfoBinding;
import com.example.yichuangyun.databinding.ItemParentBinding;
import com.example.yichuangyun.model.order.OrderDetailMoreModel;
import com.example.yichuangyun.model.order.OrderListDataModel;
import com.example.yichuangyun.mvvm.ui.activity.CompanyDetailsActivity;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.JumpIntent;

public class ParentOrderMoreAdapter extends BaseQuickAdapter<OrderDetailMoreModel, DataBindingHolder<ItemParentBinding>> {

    private final Context context;
    private static OnCallListener onCallListener;

    public ParentOrderMoreAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemParentBinding> itemParentBindingDataBindingHolder, int i, @Nullable OrderDetailMoreModel orderDetailMoreModel) {
        ItemParentBinding itemParentBinding = itemParentBindingDataBindingHolder.getBinding();
        if (orderDetailMoreModel != null) {
            switch (i) {
                case 0:
                    BaseInfoAdapter baseInfoAdapter = new BaseInfoAdapter(context);
                    itemParentBinding.recyclerView.setAdapter(baseInfoAdapter);
                    baseInfoAdapter.submitList(orderDetailMoreModel.getBaseData());
                    itemParentBinding.ivArrow.setVisibility(!orderDetailMoreModel.getBaseData().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvExpand.setVisibility(!orderDetailMoreModel.getBaseData().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvNoData.setVisibility(!orderDetailMoreModel.getBaseData().isEmpty() ? View.GONE : View.VISIBLE);
                    itemParentBinding.llTitle.setEnabled(!orderDetailMoreModel.getBaseData().isEmpty());
                    break;
                case 1:
                    FileInfoAdapter fileInfoAdapter = new FileInfoAdapter(context);
                    itemParentBinding.recyclerView.setAdapter(fileInfoAdapter);
                    fileInfoAdapter.submitList(orderDetailMoreModel.getFileInfoList());
                    itemParentBinding.ivArrow.setVisibility(!orderDetailMoreModel.getFileInfoList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvExpand.setVisibility(!orderDetailMoreModel.getFileInfoList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvNoData.setVisibility(!orderDetailMoreModel.getFileInfoList().isEmpty() ? View.GONE : View.VISIBLE);
                    itemParentBinding.llTitle.setEnabled(!orderDetailMoreModel.getFileInfoList().isEmpty());
                    break;
                case 2:
                    IncomePayAdapter incomePayAdapter = new IncomePayAdapter();
                    itemParentBinding.recyclerView.setAdapter(incomePayAdapter);
                    incomePayAdapter.submitList(orderDetailMoreModel.getIncomePayList());
                    itemParentBinding.ivArrow.setVisibility(!orderDetailMoreModel.getIncomePayList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvExpand.setVisibility(!orderDetailMoreModel.getIncomePayList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvNoData.setVisibility(!orderDetailMoreModel.getIncomePayList().isEmpty() ? View.GONE : View.VISIBLE);
                    itemParentBinding.llTitle.setEnabled(!orderDetailMoreModel.getIncomePayList().isEmpty());
                    break;
                case 3:
                    InvoiceAdapter invoiceAdapter = new InvoiceAdapter();
                    itemParentBinding.recyclerView.setAdapter(invoiceAdapter);
                    invoiceAdapter.submitList(orderDetailMoreModel.getInvoiceList());
                    itemParentBinding.ivArrow.setVisibility(!orderDetailMoreModel.getInvoiceList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvExpand.setVisibility(!orderDetailMoreModel.getInvoiceList().isEmpty() ? View.VISIBLE : View.GONE);
                    itemParentBinding.tvNoData.setVisibility(!orderDetailMoreModel.getInvoiceList().isEmpty() ? View.GONE : View.VISIBLE);
                    itemParentBinding.llTitle.setEnabled(!orderDetailMoreModel.getInvoiceList().isEmpty());
                    break;
            }

            itemParentBinding.recyclerView.setVisibility(orderDetailMoreModel.isOpen() ? View.VISIBLE : View.GONE);
            itemParentBinding.tvTitle.setText(orderDetailMoreModel.getTitle());
            itemParentBinding.ivArrow.setImageResource(orderDetailMoreModel.isOpen() ? R.mipmap.icon_down_blue : R.mipmap.icon_up_blue);
            itemParentBinding.tvExpand.setText(orderDetailMoreModel.isOpen() ? context.getString(R.string.no_expand) : context.getString(R.string.expand));

            itemParentBinding.llTitle.setOnClickListener(view -> {
                orderDetailMoreModel.setOpen(!orderDetailMoreModel.isOpen());
                notifyItemChanged(itemParentBindingDataBindingHolder.getAbsoluteAdapterPosition());
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemParentBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_parent, viewGroup);
    }

    public void setOnCallListener(OnCallListener onCallListener) {
        ParentOrderMoreAdapter.onCallListener = onCallListener;
    }

    public interface OnCallListener {
        void onCall(String phoneNumber, String businessId, String userName);
    }

    private static class BaseInfoAdapter extends BaseQuickAdapter<OrderListDataModel.OrderRecordsModel, DataBindingHolder<ItemOrderDetailBaseInfoBinding>> {
        private final Context context;

        public BaseInfoAdapter(Context context) {
            this.context = context;
        }

        @Override
        protected void onBindViewHolder(@NonNull DataBindingHolder<ItemOrderDetailBaseInfoBinding> itemOrderDetailBaseInfoBindingDataBindingHolder, int i, @Nullable OrderListDataModel.OrderRecordsModel orderRecordsModel) {
            ItemOrderDetailBaseInfoBinding binding = itemOrderDetailBaseInfoBindingDataBindingHolder.getBinding();
            binding.setBaseInfoModel(orderRecordsModel);
            if (orderRecordsModel != null) {
                binding.tvCompanyName.setEnabled(orderRecordsModel.getEnterConfirmState() == 1);
                binding.tvCompanyName.setTextColor(orderRecordsModel.getEnterConfirmState() == 1 ? context.getColor(R.color.color_426FFF) : context.getColor(R.color.color_3B3A40));
                binding.tvCompanyName.setOnClickListener(view -> {
                    if (CommonUtil.isCanClick()) {
                        Bundle bundle = new Bundle();
                        bundle.putString(Constant.creditNoKey, orderRecordsModel.getCreditNo());
                        JumpIntent.jump((Activity) context, CompanyDetailsActivity.class, bundle);
                    }
                });
                binding.tvPhone.setOnClickListener(view -> {
                    if (onCallListener != null) {
                        onCallListener.onCall(orderRecordsModel.getPhone(), orderRecordsModel.getBusinessId(), orderRecordsModel.getUsername());
                    }
                });
            }
        }

        @NonNull
        @Override
        protected DataBindingHolder<ItemOrderDetailBaseInfoBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
            return new DataBindingHolder<>(R.layout.item_order_detail_base_info, viewGroup);
        }
    }
}
